home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / lu62 / comm / int65.c < prev    next >
C/C++ Source or Header  |  1996-07-10  |  11KB  |  387 lines

  1. #include <dos.h>
  2. #include <malloc.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. /* Stack and pointer checking off */
  7. #pragma check_stack( off )
  8. #pragma check_pointer( off )
  9. #pragma intrinsic( _enable, _disable )
  10.  
  11. /* Prototypes for interrupt function */
  12. void (_interrupt _far *oldint65)( void );
  13. void (_interrupt _far *oldint8)( void );
  14. void _interrupt _far rout( unsigned _es, unsigned _ds, unsigned _di,
  15.                    unsigned _si, unsigned _bp, unsigned _sp,
  16.                    unsigned _bx, unsigned _dx, unsigned _cx,
  17.                    unsigned _ax, unsigned _ip, unsigned _cs,
  18.                    unsigned _flags );
  19. void _interrupt _far int8( unsigned _es, unsigned _ds, unsigned _di,
  20.                    unsigned _si, unsigned _bp, unsigned _sp,
  21.                    unsigned _bx, unsigned _dx, unsigned _cx,
  22.                    unsigned _ax, unsigned _ip, unsigned _cs,
  23.                    unsigned _flags );
  24. int fb(void *p);
  25. int SetRing();
  26. int rcv();
  27.  
  28. struct nau {
  29.      char name[8]    ;       /* nau name                   */
  30.      unsigned char type ;    /* type                       *//* @0115 */
  31.      struct hscb *link;      /* hscb pointer               */
  32.      struct rqb *opnq;       /* opndst queue               */
  33.      short sid;              /* session id                 */
  34.      char rsrv[3];           /* 0123 */
  35.      }naua[2];
  36.  
  37. void _far *nau_lst_ptr;
  38. void _far *hscb_lst_ptr;
  39.  
  40.  
  41. /* Huge pointers force compiler to do segment arithmetic for us. */
  42.  
  43. void _far  *InArea;
  44. char _huge *tsrstack;
  45. char _huge *svstack;
  46. char _huge *tsvstack;
  47. char _huge *tsrbottom;
  48. int  code;
  49. int  length;
  50. int  total = 0;
  51. unsigned lock = 1;
  52.  
  53. speed = 1;
  54. width = 8;
  55. parity = 2;
  56. n_port = 2;
  57. stop_num = 1;
  58.  
  59. void *fe;
  60. void *le;
  61.  
  62. char _far *dosok;
  63.  
  64. unsigned ProgPrefix;
  65.  
  66. void main( int argc, char *argv[] )
  67.         {
  68.             unsigned tsrsize;
  69.             unsigned env_seg;
  70.             unsigned *t_seg;
  71.         char _near *p;
  72.             unsigned char t;
  73.         unsigned dseg;
  74.         unsigned doff;
  75.  
  76.  
  77.             /* Initialize stack and bottom of program. */
  78.         _asm {
  79.              mov  ax, seg tsrstack
  80.              mov  es, ax
  81.              mov  WORD PTR es:tsrstack[0], sp
  82.              mov  WORD PTR es:tsrstack[2], ss
  83.                  }
  84.  
  85.         if (argc < 2) {
  86.         speed = 2;
  87.         n_port = 2;
  88.         }
  89.         else
  90.         {
  91.         n_port = argv[1][0] & 0x0f;
  92.         if (n_port > 2)
  93.             n_port = 2;
  94.         if (argc < 3) {
  95.             speed = 2;
  96.         }
  97.         else
  98.             {
  99.               t = argv[2][0];
  100.               switch( t ) {
  101.             case 'L' :
  102.             case 'l' : speed = 8;
  103.                    break;
  104.             case 'M' :
  105.             case 'm' : speed = 2;
  106.                    break;
  107.             case 'H' :
  108.             case 'h' : speed = 1;
  109.                    break;
  110.             default  : speed = 2;
  111.               }
  112.             }
  113.         }
  114.             FP_SEG( tsrbottom ) = _psp;
  115.             FP_OFF( tsrbottom ) = 0;
  116.  
  117.             ProgPrefix = _psp;   /* Save PSP value */
  118.  
  119.             /* Program size is:
  120.              *     top of stack
  121.              *   - bottom of program (converted to paragraphs)
  122.              *   + one extra paragraph
  123.              */
  124.             tsrsize = ((tsrstack - tsrbottom) >> 4) + 1;
  125.  
  126.             _asm {
  127.                 mov  ah, 3                ; check function
  128.                 int  6eh
  129.                 mov  byte ptr t[0], al    ; was int6e routine set ?
  130.             }
  131.             if (t != 0x55) {
  132.                 printf("I can't find Async. driver.\n");
  133.                 printf("Async. driver must be installed!\n");
  134.                 return 0;
  135.             }
  136.  
  137.             if (SetRing() == -1) {
  138.                 printf("No core to install INT65.\n");
  139.                 return 0;
  140.             }
  141.  
  142.  
  143.             /* Initialization NAU */
  144.             memcpy(naua[0].name,"LU1     ", 8);
  145.             memcpy(naua[1].name,"LU2     ", 8);
  146.             naua[0].opnq = NULL;
  147.             naua[1].opnq = NULL;
  148.             naua[0].sid = 0;
  149.             naua[1].sid = 0;
  150.             hscb_lst_ptr = NULL;
  151.             nau_lst_ptr = &naua[0];
  152.  
  153.             _asm {
  154.                 mov   ax, 3400h
  155.         int   21h
  156.                 mov   word ptr doff[0], bx
  157.                 mov   ax, es
  158.                 mov   word ptr dseg[0], ax
  159.             }
  160.  
  161.             FP_SEG( dosok ) = dseg;
  162.             FP_OFF( dosok ) = doff;
  163.  
  164.             printf("Mini-Port now installed.\n");
  165.  
  166.             /* Replace existing int65 & int8 routine with our. */
  167.             _asm cli
  168.          oldint8 = _dos_getvect( 0x8 );
  169.             _dos_setvect( 0x8, int8 );
  170.             oldint65 = _dos_getvect( 0x65 );
  171.             _dos_setvect( 0x65, rout );
  172.  
  173.             /* Free the PSP segment and terminate with program resident. */
  174.          _dos_keep( 0, tsrsize );
  175.         }
  176.  
  177. void _interrupt _far rout( unsigned _es, unsigned _ds, unsigned _di,
  178.                    unsigned _si, unsigned _bp, unsigned _sp,
  179.                    unsigned _bx, unsigned _dx, unsigned _cx,
  180.                    unsigned _ax, unsigned _ip, unsigned _cs,
  181.                                unsigned _flags )
  182. {
  183.  
  184.       FP_OFF( InArea ) = _dx;
  185.       FP_SEG( InArea ) = _ds;
  186.       code = _ax;
  187.       length = _cx;
  188.       _asm {
  189.                mov  ax, seg svstack
  190.                mov  es, ax
  191.                mov  WORD PTR es:svstack[0], sp
  192.                mov  WORD PTR es:svstack[2], ss
  193.                mov  ax, seg tsrstack
  194.                mov  es, ax
  195.                mov  sp, WORD PTR es:tsrstack[0]
  196.                mov  ss, WORD PTR es:tsrstack[2]
  197.            }
  198.  
  199.       if (lock) {
  200.             lock = 0;
  201.             switch (code) {
  202.           case 0:  appl(InArea);
  203.                        break;
  204.               case 1:  if (!total) {
  205.                           rcv();
  206.                        }
  207.                        break;
  208.               case 2:  fb(InArea);
  209.                        break;
  210.             }
  211.             _asm cli
  212.             lock = 1;
  213.       }
  214.       _asm {
  215.                cli
  216.                mov  ax, seg svstack
  217.                mov  es, ax
  218.                mov  sp, WORD PTR es:svstack[0]
  219.                mov  ss, WORD PTR es:svstack[2]
  220.            }
  221.  }
  222.  
  223. int fb(char *p)
  224. {
  225.         register int rest;
  226.         char *t;
  227.         struct ce {
  228.                      struct ce *next;
  229.                      unsigned desc;
  230.              int lt;
  231.              char b[120];
  232.                    } *re;
  233.  
  234.         re = le; /* last element */
  235.         t = p;
  236.         rest = length;
  237.         while (rest) {
  238.             if (rest > 120) {
  239.                 memcpy(re -> b, p, 120);
  240.                 re -> desc = 1; /* middle segment */
  241.                 re -> lt = 120;
  242.                 p += 120;
  243.                 rest -= 120;
  244.                 re = re -> next;
  245.             }
  246.             else {
  247.            memcpy(re -> b, p, rest);
  248.                    re -> desc = 0; /* end or single segment */
  249.                    re -> lt = rest;
  250.                    rest = 0;
  251.                  }
  252.         }
  253.         le = re -> next; /* Reset pointer to last element */
  254.         total = 1;
  255.         return 0;
  256. }
  257.  
  258. int rcv()
  259. {
  260.         struct ce {
  261.                      struct ce *next;
  262.                      unsigned desc;
  263.              int lt;
  264.              char b[120];
  265.                    } *re;
  266.         register int i;
  267.         register unsigned dsc;
  268.         char *p;
  269.         char *t;
  270.  
  271.  
  272.         re = fe; /* first element of the buffer ring */
  273.   Repete:
  274.         i = 0;
  275.         do {
  276.                 i += re -> lt;
  277.                 dsc = re -> desc;
  278.                 re = re -> next;
  279.         } while (dsc);
  280.  
  281.         if ((p = malloc(i)) == NULL) {
  282.             printf("No memory for make receive function.\n");
  283.             return 0;
  284.         }
  285.  
  286.         re = fe; /* first element of the buffer ring */
  287.         t = p;
  288.         do {
  289.                 memcpy(t, re -> b, re -> lt);
  290.                 t += re -> lt;
  291.                 dsc = re -> desc;
  292.                 re = re -> next;
  293.         } while (dsc);
  294.  
  295.         appl(p + 12);
  296.         free( p );
  297.         fe = re;
  298.  
  299.         if (le == fe) {
  300.                 total = 0;
  301.         }
  302.         else
  303.                 goto Repete;
  304.  
  305.         return 0;
  306. }
  307.  
  308. /*
  309.  * Set ring of the input buffers.
  310.  * Input buffers will be fills by Async. exit
  311.  * subruotine.
  312.  */
  313.  
  314. int SetRing()
  315. {
  316.         /* Element of the buffer ring */
  317.  
  318.         struct ce {
  319.                      struct ce *next;
  320.                      unsigned desc;
  321.                      int lt;
  322.                      char b[120];
  323.                    } *re;
  324.  
  325.         struct ce *first;
  326.         struct ce *temp;
  327.         register int i;
  328.  
  329.     if ((temp = malloc(sizeof(struct ce))) == NULL) {
  330.             return -1;
  331.         }
  332.         first = temp;
  333.  
  334.         for (i = 0; i < 18; i++) {
  335.         if ((re = malloc(sizeof(struct ce))) == NULL) {
  336.                 break;
  337.             }
  338.             else  {
  339.                      temp -> next = re;
  340.                      temp = re;
  341.                   }
  342.         }
  343.         re -> next = first;   /* Complete the ring */
  344.  
  345.         fe = first;  /* first element of the ring */
  346.         le = first;  /* last element of the ring */
  347.         return 0;
  348. }
  349.  
  350. void _interrupt _far int8( unsigned _es, unsigned _ds, unsigned _di,
  351.                    unsigned _si, unsigned _bp, unsigned _sp,
  352.                    unsigned _bx, unsigned _dx, unsigned _cx,
  353.                    unsigned _ax, unsigned _ip, unsigned _cs,
  354.                                unsigned _flags )
  355. {
  356.  
  357.        (*oldint8)();
  358.        _disable();
  359.         if ((total != 0) && (lock != 0)) {
  360.                 if (*dosok == 0) {
  361.                         _asm {
  362.                                  mov  ax, seg tsvstack
  363.                                  mov  es, ax
  364.                                  mov  WORD PTR es:tsvstack[0], sp
  365.                                  mov  WORD PTR es:tsvstack[2], ss
  366.                                  mov  ax, seg tsrstack
  367.                                  mov  es, ax
  368.                                  mov  sp, WORD PTR es:tsrstack[0]
  369.                                  mov  ss, WORD PTR es:tsrstack[2]
  370.                          }
  371.                         lock = 0;
  372.                         _asm sti
  373.                         rcv();
  374.                         _asm cli
  375.                         lock = 1;
  376.                         _disable();
  377.                         _asm {
  378.                                  mov  ax, seg tsvstack
  379.                                  mov  es, ax
  380.                                  mov  sp, WORD PTR es:tsvstack[0]
  381.                                  mov  ss, WORD PTR es:tsvstack[2]
  382.                                  mov  bp, sp
  383.                              }
  384.                 }
  385.     }
  386. }
  387.